package top.simonby.iron.plugin.data.jpa.query;


import org.springframework.data.jpa.domain.Specification;
import top.simonby.iron.plugin.data.support.BaseQuery;

/**
 * @author Simon
 * @date 2020/4/23 20:51
 */
@SuppressWarnings("ALL")
public abstract class SpecBaseQuery<T> extends BaseQuery {


    /**
     * 将查询转换成Specification
     *
     * @return
     */
    public abstract Specification<T> toSpec();

    /**
     * 动态查询and连接
     *
     * @return
     */
    protected Specification<T> toSpecWithAnd() {
        return this.toSpecWithLogicType("and", this);
    }

    /**
     * 动态查询or连接
     *
     * @return
     */
    protected Specification<T> toSpecWithOr() {
        return this.toSpecWithLogicType("or", this);
    }

    /**
     * logicType or/and
     *
     * @param logicType
     * @return
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public <M extends SpecBaseQuery> Specification<T> toSpecWithLogicType(String logicType, M baseQuery) {
        SpecGenerator.DefaultSpecGenerator<M> generator = new SpecGenerator.DefaultSpecGenerator<M>() {
        };
        return generator.builderSpecification(logicType, baseQuery);
    }


}